package tintuc.songkhoe;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdView;
import com.google.ads.InterstitialAd;
import com.google.ads.AdRequest.ErrorCode;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class MainActivity extends Activity implements AdListener {
	WebView web;
	ProgressBar loading;
	private InterstitialAd interstitial;
	AdRequest adRequest;
	 ProgressDialog taitrang;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		web = (WebView) findViewById(R.id.wview);
		loading = (ProgressBar) findViewById(R.id.progressBar);
		interstitial = new InterstitialAd(this,
				"ca-app-pub-5749889588405632/1398583100");
		adRequest = new AdRequest();
		interstitial.setAdListener(this);
		AdView adView = (AdView) this.findViewById(R.id.adView);
		adView.loadAd(new AdRequest());
		if (checkIntenet()) {
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/");
			Handler han = new Handler();
			han.postDelayed(new Runnable() {
				@Override
				public void run() {
					// TODO Auto-generated method stub
					interstitial.loadAd(adRequest);

				}
			}, 6000);

		} else {
			AlertDialog thongbao = new AlertDialog.Builder(MainActivity.this)
					.create();
			thongbao.setTitle("Thông Báo");
			thongbao.setMessage("Bạn chưa kết nối mạng!");
			thongbao.setButton("OK", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			thongbao.show();
		}

	}

	public final boolean checkIntenet() {
		ConnectivityManager connectmanager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		if (connectmanager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
				|| connectmanager.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTING
				|| connectmanager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTING
				|| connectmanager.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
			return true;
		} else if (connectmanager.getNetworkInfo(0).getState() == NetworkInfo.State.DISCONNECTED
				|| connectmanager.getNetworkInfo(1).getState() == NetworkInfo.State.DISCONNECTED) {
			return false;
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.itemtrangchu:
			loadingProgress();
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/");
			return true;
		case R.id.itemBacsytuvan:
			this.loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-bac-si-tu-van.html");
			return true;
		case R.id.itemVuikhoe:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-vui-khoe.html");
			return true;
		case R.id.itemDinhduong:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-dinh-duong.html");
			return true;
		case R.id.itemKienthuc:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-kien-thuc.html");
			return true;
		case R.id.itemThoisu:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-thoi-su.html");
			return true;
		case R.id.itemLamdep:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-lam-dep.html");
			return true;
		case R.id.itemvideo:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-kien-thuc.html");
			return true;
		case R.id.itemGioitinh:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-gioi-tinh.html");
			return true;
		case R.id.itemMevaBe:
			loadingProgress();
			loading.setVisibility(View.GONE);
			web.setWebViewClient(new clientAsyn());
			web.getSettings().setJavaScriptEnabled(true);
			web.loadUrl("http://songkhoe.vn/chuyen-muc-me-va-be.html");
			return true;
		}

		return super.onOptionsItemSelected(item);
	}

	public void loadingProgress() {
		taitrang = new ProgressDialog(MainActivity.this);
		taitrang.setMessage("Chờ chút...");
		taitrang.setIndeterminate(true);
		taitrang.show();
		taitrang.setCancelable(true);
		new Thread(new Runnable() {
			@Override
			public void run() {
				try {
					Thread.sleep(1000);
				} catch (Exception e) {

				}
				taitrang.dismiss();
			}
		}).start();
	}

	public class clientAsyn extends WebViewClient {
		@Override
		public void onPageStarted(WebView view1, String url1, Bitmap favicon1) {
			super.onPageStarted(view1, url1, favicon1);
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}

		@Override
		public boolean shouldOverrideUrlLoading(WebView view1, String url1) {
			view1.loadUrl(url1);
			return true;

		}

		@Override
		public void onPageFinished(WebView view, String url) {
			super.onPageFinished(view, url);
			view.loadUrl("javascript:document.getElementById('bottom_header').remove();document.getElementById('top_footer').remove();document.getElementById('bottombar').remove();");
			loading.setVisibility(View.GONE);
		}
	}

	@Override
	public boolean onKeyDown(int keyCode1, KeyEvent event1) {
		if ((keyCode1 == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
			web.goBack();
			return true;
		}
		return super.onKeyDown(keyCode1, event1);
	}

	public void onDismissScreen(Ad arg0) {
		// TODO Auto-generated method stub

	}

	public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
		// TODO Auto-generated method stub

	}

	public void onLeaveApplication(Ad arg0) {
		// TODO Auto-generated method stub

	}

	public void onPresentScreen(Ad arg0) {
		// TODO Auto-generated method stub

	}

	public void onReceiveAd(Ad arg0) {
		// TODO Auto-generated method stub
		Log.d("OK", "Received ad");
		if (arg0 == interstitial) {
			interstitial.show();
		}
	}

}
